package com.gz.mylife.app;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.util.ImageUtils;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXAppExtendObject;
import com.tencent.mm.sdk.modelmsg.WXImageObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ErweimaActivity extends BaseActivity {
	ImageView view;
	private IWXAPI mIwxapi;
	private Dialog mLoadingData;
	private TextView tvInNew;//立即分享按钮

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_erweima);
		setActionbarTitle("扫一扫", false);

		mIwxapi = WXAPIFactory.createWXAPI(getApplicationContext(), Define.APP_ID_WECHAT, true);
		mIwxapi.registerApp(Define.APP_ID_WECHAT);
		mLoadingData = new Dialog(this, R.style.DialogTheme);
		mLoadingData.setContentView(R.layout.loading_data_dialog2);
		mLoadingData.setCanceledOnTouchOutside(false);
		tvInNew = (TextView)findViewById(R.id.tvInNew);
		tvInNew.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mLoadingData.show();
				//				Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.erweima);
				//				WXImageObject imgObj = new WXImageObject(bmp);
				//
				//				WXAppExtendObject a = new WXAppExtendObject();
				//				WXMediaMessage msg = new WXMediaMessage();
				//				msg.mediaObject = imgObj;
				//
				//				Bitmap smartBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
				//				bmp.recycle();
				//				msg.thumbData =ImageUtils.bmpToByteArray(smartBmp,true);
				//				
				//				SendMessageToWX.Req req = new SendMessageToWX.Req();
				//				req.transaction = buildTransaction("img");
				//				req.message = msg;
				//				req.scene = SendMessageToWX.Req.WXSceneTimeline;
				//				mIwxapi.sendReq(req);
				//				finish();
				Toast.makeText(ErweimaActivity.this,"开始分享",Toast.LENGTH_LONG).show();
				UMImage image = new UMImage(ErweimaActivity.this, R.drawable.erweima);//资源文件
				new ShareAction(ErweimaActivity.this)
				.setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
				.withText("欢迎使用双明汇")//分享内容
				.withMedia(image)
				.setCallback(new UMShareListener() {

					@Override
					public void onStart(SHARE_MEDIA arg0) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onResult(SHARE_MEDIA arg0) {
						// TODO Auto-generated method stub
						Toast.makeText(ErweimaActivity.this,"分享成功",Toast.LENGTH_LONG).show();
						finish();

					}

					@Override
					public void onError(SHARE_MEDIA arg0, Throwable arg1) {
						// TODO Auto-generated method stub
						Toast.makeText(ErweimaActivity.this,"分享失败",Toast.LENGTH_LONG).show();
						finish();
					}

					@Override
					public void onCancel(SHARE_MEDIA arg0) {
						// TODO Auto-generated method stub
						Toast.makeText(ErweimaActivity.this,"分享取消",Toast.LENGTH_LONG).show();

					}
				})//回调监听器
				.share();

			}
		});

		view = (ImageView)findViewById(R.id.activity_erweima_image);

	}

	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(mLoadingData.isShowing()) {
			mLoadingData.dismiss();
		}
	}

	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}
}
